package com.adam.zk.service;

import com.adam.zk.model.Food;
import com.adam.zk.repository.FoodRepository;
import lombok.extern.slf4j.Slf4j;
import org.joda.money.Money;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
@CacheConfig(cacheNames = "FoodCache")
public class FoodService {
    @Autowired
    private FoodRepository foodRepository;

    public Food saveFood(String name, Money price) {
        return foodRepository.save(Food.builder().name(name).price(price).build());
    }

    @Cacheable
    public List<Food> getAllFood() {
        return foodRepository.findAll(Sort.by("id"));
    }

    public Food getFood(Long id) {
        return foodRepository.getOne(id);
    }

    public long getFoodCount() {
        return foodRepository.count();
    }

    public Food getFood(String name) {
        return foodRepository.findByName(name);
    }

    public List<Food> getFoodByName(List<String> names) {
        return foodRepository.findByNameInOrderById(names);
    }
}
